﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public partial class UIABFQC:UIABF
    {
        
        #region ==QPKRS===========
        public static readonly DependencyProperty QPKRSProperty = DependencyProperty.Register("QPKRS",
            typeof(string), typeof(UIABFQC),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQPKRSChanged));
        public string QPKRS
        {
            get
            {
                return (string)GetValue(QPKRSProperty);
            }
            set
            {
                SetValue(QPKRSProperty, value);
                ((VABFOLGE)MV).QPKRS = value;
            }
        }
        private static void OnQPKRSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABFQC;
            if (src != null)
            {
                src.QPKRS = (string)e.NewValue;
            }
        }
        #endregion

        #region ==QPLAN===========
        public static readonly DependencyProperty QPLANProperty = DependencyProperty.Register("QPLAN",
            typeof(string), typeof(UIABFQC),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQPLANChanged));
        public string QPLAN
        {
            get
            {
                return (string)GetValue(QPLANProperty);
            }
            set
            {
                SetValue(QPLANProperty, value);
                ((VABFOLGE)MV).QPLAN = value;
            }
        }
        private static void OnQPLANChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABFQC;
            if (src != null)
            {
                src.QPLAN = (string)e.NewValue;
            }
        }
        #endregion

        #region ==GSART===========
        public static readonly DependencyProperty GSARTProperty = DependencyProperty.Register("GSART",
            typeof(string), typeof(UIABFQC),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnGSARTChanged));
        public string GSART
        {
            get
            {
                return (string)GetValue(GSARTProperty);
            }
            set
            {
                SetValue(GSARTProperty, value);
                ((VABFOLGE)MV).GSART = value;
            }
        }
        private static void OnGSARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABFQC;
            if (src != null)
            {
                src.GSART = (string)e.NewValue;
            }
        }
        #endregion

        #region ==YCOUNT===========
        public static readonly DependencyProperty YCOUNTProperty = DependencyProperty.Register("YCOUNT",
            typeof(string), typeof(UIABFQC),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnYCOUNTChanged));
        public string YCOUNT
        {
            get
            {
                return (string)GetValue(YCOUNTProperty);
            }
            set
            {
                SetValue(YCOUNTProperty, value);
               
            }
        }
        private static void OnYCOUNTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABFQC;
            if (src != null)
            {
                src.YCOUNT = (string)e.NewValue;
            }
        }
        #endregion

        #region ==GPPARTIE===========
        public static readonly DependencyProperty GPPARTIEProperty = DependencyProperty.Register("GPPARTIE",
            typeof(string), typeof(UIABFQC),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnGPPARTIEChanged));
        public string GPPARTIE
        {
            get
            {
                return (string)GetValue(GPPARTIEProperty);
            }
            set
            {
                SetValue(GPPARTIEProperty, value);
            }
        }
        private static void OnGPPARTIEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABFQC;
            if (src != null)
            {
                src.GPPARTIE = (string)e.NewValue;
            }
        }
        #endregion
        #region ==TYPE===========
        public static readonly DependencyProperty TYPEProperty = DependencyProperty.Register("TYPE",
            typeof(string), typeof(UIABFQC),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTYPEChanged));
        public string TYPE
        {
            get
            {
                return (string)GetValue(TYPEProperty);
            }
            set
            {
                SetValue(TYPEProperty, value);
            }
        }
        private static void OnTYPEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABFQC;
            if (src != null)
            {
                src.TYPE = (string)e.NewValue;
            }
        }
        #endregion
       

        #region ==GPPARTIEC===========
        public static readonly DependencyProperty GPPARTIECProperty = DependencyProperty.Register("GPPARTIEC",
            typeof(string), typeof(UIABFQC),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnGPPARTIECChanged));
        public string GPPARTIEC
        {
            get
            {
                return (string)GetValue(GPPARTIECProperty);
            }
            set
            {
                SetValue(GPPARTIECProperty, value);
            }
        }
        private static void OnGPPARTIECChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABFQC;
            if (src != null)
            {
                src.GPPARTIEC = (string)e.NewValue;
            }
        }
        #endregion

        #region ==PARTIE===========
        public static readonly DependencyProperty PARTIEProperty = DependencyProperty.Register("PARTIE",
            typeof(string), typeof(UIABFQC),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPARTIEChanged));
        public string PARTIE
        {
            get
            {
                return (string)GetValue(PARTIEProperty);
            }
            set
            {
                SetValue(PARTIEProperty, value);
                ((VABFOLGE)MV).PARTIE = value;
            }
        }
        private static void OnPARTIEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABFQC;
            if (src != null)
            {
                src.PARTIE = (string)e.NewValue;
            }
        }
        #endregion

      

        #region ==FROMDATE===========
        public static readonly DependencyProperty FROMDATEProperty = DependencyProperty.Register("FROMDATE",
            typeof(string), typeof(UIABFQC),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFROMDATEChanged));
        public string FROMDATE
        {
            get
            {
                return (string)GetValue(FROMDATEProperty);
            }
            set
            {
                SetValue(FROMDATEProperty, value);
            }
        }
        private static void OnFROMDATEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABFQC;
            if (src != null)
            {
                src.FROMDATE = (string)e.NewValue;
            }
        }
        #endregion

        #region ==ENDDATE===========
        public static readonly DependencyProperty ENDDATEProperty = DependencyProperty.Register("ENDDATE",
            typeof(string), typeof(UIABFQC),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnENDDATEChanged));
        public string ENDDATE
        {
            get
            {
                return (string)GetValue(ENDDATEProperty);
            }
            set
            {
                SetValue(ENDDATEProperty, value);
            }
        }
        private static void OnENDDATEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABFQC;
            if (src != null)
            {
                src.ENDDATE = (string)e.NewValue;
            }
        }
        #endregion
         
        #region ==QCDOCTYP===========
        public static readonly DependencyProperty QCDOCTYPProperty = DependencyProperty.Register("QCDOCTYP",
            typeof(string), typeof(UIABFQC),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQCDOCTYPChanged));
        public string QCDOCTYP
        {
            get
            {
                return (string)GetValue(QCDOCTYPProperty);
            }
            set
            {
                SetValue(QCDOCTYPProperty, value);
                if (MV == null)
                    return;
                ((VABFOLGE)MV).QCDOCTYP = value;
            }
        }
        private static void OnQCDOCTYPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABFQC;
            if (src != null)
            {
                src.QCDOCTYP = (string)e.NewValue;
            }
        }
        #endregion

        #region ==ABF===========
        public static readonly DependencyProperty ABFProperty = DependencyProperty.Register("ABF",
            typeof(string), typeof(UIABFQC),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnABFChanged));
        public string ABF
        {
            get
            {
                return (string)GetValue(ABFProperty);
            }
            set
            {
                SetValue(ABFProperty, value);
            }
        }
        private static void OnABFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABFQC;
            if (src != null)
            {
                src.ABF = (string)e.NewValue;
            }
        }
        #endregion

      

        #region ==IsQC===========
        public static readonly DependencyProperty IsQCProperty = DependencyProperty.Register("IsQC",
            typeof(bool), typeof(UIABFQC),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsQCChanged));
        public bool IsQC
        {
            get
            {
                return (bool)GetValue(IsQCProperty);
            }
            set
            {
                SetValue(IsQCProperty, value);
            }
        }
        private static void OnIsQCChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABFQC;
            if (src != null)
            {
                src.IsQC = (bool)e.NewValue;
            }
        }
        #endregion

      
         public TDRAUFTR cDR
        {
            get
            {
              return (TDRAUFTR)this.FindName("cDR");
            }
        }
        public UIABFQC()
        { 
            MESSAGES += "QCRELOAD;REFRESHBYGB33;";
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
        }
        public override void OnWorkflowIDChange(ReflectionStudio.Core.Events.MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {
                case "QCRELOAD":
                    TMV.QCRELOAD(msg);
                    break;
                case "REFRESHBYGB33":
                    TMV.REFRESHBYGB33(msg);
                    break;
            }


        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
          
            if (!IsQC)
                return;
            LOADGP();
        }
        public void LOADGP()
        {
           
            MessageInfo msg = new MessageInfo();
            msg.MSGTYPE = "LOADGP";
            msg.Sender = this.RUNID;
            msg.ACTION = "LOADGP";

            if (dwABF == null)
            {
                msg.PARTIE = "";
            }
            else
            {
                msg.ORDER = dwABF["QOKORDER"].ToString();
                msg.PARTIE = dwABF["GPPARTIE"].ToString();
                msg.P5 = "G";
                msg.AUFTYP = dwABF["ABAAUFTYP"].ToString();
                msg.KRS = dwABF["ABFAUFKRS"].ToString();
                msg.LFD = dwABF["ABFAUFLFD"].ToString();
                msg.POS = dwABF["ABFAUFPOS"].ToString();
                msg.LIEFSCH = dwABF["ABFAFOLGE"].ToString();
                msg.ART = dwABF["ABPART"].ToString();
                msg.VAR = dwABF["ABPVAR"].ToString();
                msg.FARBE = dwABF["ABPFARBE"].ToString();
                msg.DESS = dwABF["ABPDESS"].ToString();
                msg.COLORIT = dwABF["ABPCOLORIT"].ToString();
                msg.BESCHICH = dwABF["ABPBESCHICH"].ToString();
                msg.GROE = dwABF["ABPGROE"].ToString();
                msg.P1 = dwABF["YCOUNT"].ToString();
                DataSet Set = new DataSet();
                DataTable TB = TBMain.Clone();
                dwData = TB.Rows.Add();
                dwData.ItemArray = dwABF.ItemArray;
                Set.Merge(TB);
                msg.Set = Set;
            }
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                 
                //GPPARTIE GPGART GPGFARBE GPBEM1 GPBEM2
                
                dwData = TBMain.Clone().NewRow();
                dwData["ABFMANDANT"] = app.Mandant;
                dwData["ABFKSTKRS"] = ART;
                dwData["ABFKST"] = FARBE;
                dwData["ABFPLANKZ"] = GPPARTIE;
                dwData["ABFNEUBEN"] = YCOUNT;
                if (GPPARTIE.Trim() == "00000000")
                {
                    dwData["ABFPLANKZ"] = "0";
                } 
                dwData["ABFAGANG"] = AGANG;
                dwData["ABFBZAGANG"] = "";
                dwData["ABFFILLER"] = VQPMERKM.Instance.TTLQT();
                if (GPPARTIE.Trim() != "")
                {
                    SResult =TMV.QABF10(dwData);
                }
                else
                {
                    SResult = TMV.QABF12(dwData);
                }
                //纱支返回字段
                MergeData(SResult, false);
                IsEdit = false;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwABF == null)
                {
                    MessageBox.Show("请选择要检验的批号才能建立质检单！");
                    return;
                }
                //btnNEW.IsEnabled = false;
                //IsEdit = true;
                //base.NewCommand(sender, e);
                string ORD = dwABF["QOKORDER"].ToString();

                PM["RUNID"] = RUNID;
                PM["ORDER"] = ORD;
                PM["PARTIE"] = dwABF["GPPARTIE"].ToString();
                PM["P5"] = "G";
                PM["AUFTYP"] = dwABF["ABAAUFTYP"].ToString();
                PM["KRS"] = dwABF["ABFAUFKRS"].ToString();
                PM["LFD"] = dwABF["ABFAUFLFD"].ToString();
                PM["POS"] = dwABF["ABFAUFPOS"].ToString();
                PM["LIEFSCH"] = dwABF["ABFAFOLGE"].ToString();
                PM["ART"] = dwABF["ABPART"].ToString();
                PM["VAR"] = dwABF["ABPVAR"].ToString();
                PM["FARBE"] = dwABF["ABPFARBE"].ToString();
                PM["DESS"] = dwABF["ABPDESS"].ToString();
                PM["COLORIT"] = dwABF["ABPCOLORIT"].ToString();
                PM["BESCHICH"] = dwABF["ABPBESCHICH"].ToString();
                PM["GROE"] = dwABF["ABPGROE"].ToString();
                PM["YCOUNT"] = dwABF["YCOUNT"].ToString();
                DataSet Set = new DataSet();
                DataTable TB = TBMain.Clone();
                dwData = TB.Rows.Add();
                dwData.ItemArray = dwABF.ItemArray;
                Set.Merge(TB);

                PM["QPLAN"] = QPLAN;
                PM["ROWQP"] = Set.Tables[0].Rows[0];
                
                VQORDER.Instance.QCNEW(PM);

                dwData = VABAUFM.Instance.TBMain.NewRow();
                dwData["ABAMANDANT"] = dwABF["ABAMANDANT"].ToString();
                dwData["ABAAUFKRS"] = dwABF["ABFAUFKRS"].ToString();
                dwData["ABAAUFLFD"] = dwABF["ABFAUFLFD"].ToString();
                dwData["ABAAUFPOS"] = dwABF["ABFAUFPOS"].ToString();
                dwData["ABAKDPARTIE"] = dwABF["GPPARTIE"].ToString();
                VABAUFM.Instance.UpdateABAUFM(dwData, 9);
                if (ORD != "000000000")
                {
                    QueryCommand(null, null);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            if (dwABF == null)
            {
                MessageBox.Show("请选择要修改记录！");
                return;
            }
            if (!IsEdit)
            {
                MV.TBTemp = TBMain.Copy();
            }
            else
            {
                MV.TBTemp = null;
            }
            //base.EditCommand(sender, e);

            IsEdit = true;

        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {

            ((VABFOLGE)MV).BATCHGBCHG();
            base.SaveCommand(sender, e);
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwABF == null)
                {
                    MessageBox.Show("请选择要删除的质检单！");
                    return;
                }

                dwData = VQORDER.Instance.TBMain.NewRow();
                dwData["QOKMANDANT"] = app.Mandant;
                dwData["QOKORDER"] = dwABF["QOKORDER"];
                VQORDER.Instance.DeleteQORDER(dwData, 1);
                dwData = VQOSTEP.Instance.TBMain.NewRow();
                dwData["QOEMANDANT"] = app.Mandant;
                dwData["QOEORDER"] = dwABF["QOKORDER"];
                VQOSTEP.Instance.DeleteQOSTEP(dwData, 2);
                dwData = VABAUFM.Instance.TBMain.NewRow();
                dwData["ABAMANDANT"] = dwABF["ABAMANDANT"].ToString();
                dwData["ABAAUFKRS"] = dwABF["ABFAUFKRS"].ToString();
                dwData["ABAAUFLFD"] = dwABF["ABFAUFLFD"].ToString();
                dwData["ABAAUFPOS"] = dwABF["ABFAUFPOS"].ToString();
                dwData["ABAKDPARTIE"] = dwABF["GPPARTIE"].ToString();
                VABAUFM.Instance.UpdateABAUFM(dwData, 10);

                QueryCommand(null, null);

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public void QUERY_CLICK(object sender, RoutedEventArgs e)
        {
            try
            {
                SResult = new DataSet();
                DataSet SR = new DataSet();
                if (GPPARTIEC != "")
                {
                    GPPARTIEC = GPPARTIEC.Replace(",","','");
                    GPPARTIEC = GPPARTIEC.Replace("，","','");
                    GPPARTIEC = GPPARTIEC.Replace(" ", "");
                    GPPARTIEC = GPPARTIEC.ToUpper();
                }
                dwData = TBMain.Clone().NewRow();
                dwData["ABFMANDANT"] = app.Mandant;
                dwData["ABFPLANKZ"] = GPPARTIE;
                dwData["ABFRUESTFAM"] = FARBE;
                dwData["ABFAGANG"] = AGANG;
                dwData["ABFMNRKRS"] = GPPARTIEC;
                dwData["ABFNEUBEN"] = YCOUNT;
                dwData["ABFFILLER"] = VQPMERKM.Instance.TTLQT();
               if (GPPARTIE == "")
                {
                    dwData["ABFPLANKZ"] = "AAA";
                }
                if (GPPARTIEC == "")
                {
                    dwData["ABFMNRKRS"] = "BBB";
                }
                if (FROMDATE == "")
                {
                    dwData["ABFNEUDAT"] = "20000000";
                }
                else
                {
                    dwData["ABFNEUDAT"] = FROMDATE;
                }
                if (ENDDATE == "")
                {
                    dwData["ABFAENDDAT"] = "99999999";
                }
                else
                {
                    dwData["ABFAENDDAT"] = ENDDATE;
                }
                if (TYPE == "")
                {
                    dwData["ABFMTYP"] = "AAA";
                }
                else
                {

                    dwData["ABFMTYP"] = TYPE;
                }
                SResult = TMV.QABF19(dwData);                
                //SResult = ((VABFOLGE)MV).QueryABFOLGE(dwData, 13);

                SR.Merge(SResult);
                MergeData(SR, false);
                SResult = TMV.QABF20(dwData);
                if (SResult.Tables.Count > 0)
                {
                    SR.Merge(SResult);
                    MergeData(SR, false);
                }

                GPPARTIEC = GPPARTIEC.Replace("','", ","); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void PrintCommand(object sender, RoutedEventArgs e)
        {
            try
            { 
               
                cDR.DocType = QCDOCTYP;
                cDR.PRINT(MV.SetProd);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public void QUERY_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.Focus();
                QueryCommand(null, null);
            }

        }

        public void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            { 
                FROMDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        public void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            { 
                ENDDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
    }
}
